package com.group02.community.controller;

import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.dept.SysDept;
import com.group02.community.entity.vo.UserDeptVO;
import com.group02.community.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
@CrossOrigin
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @ApiOperation("查询所有岗位")
    @GetMapping("/dept")
    public ResponseEntity<List<SysDept>> getAllDept() {
        System.out.println("执行了查询,");
        List<SysDept> sysDepts = deptService.selectAllDept();
        return ResponseEntity.ok(sysDepts);
    }

    @ApiOperation("部门添加")
    @PostMapping("/add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    public ResponseEntity<String> addDept(@RequestBody SysDept dept) {
        dept.setCreateBy("admin");
        long pid = dept.getParentId();
        if (pid != 100) {
            dept.setAncestors("0,100," + pid);
        } else {
            dept.setAncestors("0,100");
        }
        System.out.println("执行了添加");
//        dept.setCreateTime(LocalDateTime.now());
        deptService.save(dept);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("部门修改")
    @PutMapping("/update")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    public ResponseEntity<String> updateDept(@RequestBody SysDept dept) {
        dept.setUpdateBy("admin");
        long pid = dept.getParentId();
        long deptId = dept.getDeptId();
        Long num = deptService.checkDeptExistUser(deptId);
        Long aLong = deptService.selectNormalChildrenDeptById(deptId);
        if (pid == 0) {
            if (deptId == 100) {
                dept.setAncestors("0");
                dept.setParentId(0);
            } else {
                dept.setAncestors("0,100");
                dept.setParentId(100);
            }
        } else if (pid != 100) {
            dept.setAncestors("0,100," + pid);

        } else {
            dept.setAncestors("0,100");
        }
        if (dept.getStatus().equals("1")) {
            if (num != 0) {
                return ResponseEntity.ok("no");
            }
            if (aLong != 0) {
                System.out.println(aLong);
                //状态修改下级是否有正常部门
                return ResponseEntity.ok("NO");
            }
        }
        System.out.println("执行了修改");
//        dept.setUpdateTime(LocalDateTime.now());
        deptService.updateById(dept);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("岗位删除")
    @DeleteMapping("/delete/{id}")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public ResponseEntity<String> deleteDept(@PathVariable Long id) {
        Long num = deptService.checkDeptExistUser(id);
        Long dId = deptService.hasChildByDeptId(id);
        System.out.println(num);
        if (dId != 0) {
            return ResponseEntity.ok("NO");
        }
        if (num != 0) {
            return ResponseEntity.ok("no");
        }

        deptService.delete(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("模糊查询")
    @GetMapping("/queryPage")
    public ResponseEntity<List<SysDept>> getQueryPage(String deptName, String status) {
        List<SysDept> queryPage = deptService.getQueryPage(deptName, status);
        return ResponseEntity.ok(queryPage);
    }


    //*************************************以下为zzx代码*****************************************

    @GetMapping("dept/depts")
    public ResponseEntity<List<UserDeptVO>> getDept() {
        List<UserDeptVO> allDept = deptService.getAllDept();
        return ResponseEntity.ok(allDept);
    }

}
